package com.github.szhittech.service.task;


import com.github.szhittech.MainPlugin;
import com.github.szhittech.service.task.impl.UploadTask;
import com.github.szhittech.util.Logc;

import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectList;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.maven.MavenResolver;

public class TaskApi {
    public final static String taskName = "uploadArchives";

    public static void init(MainPlugin mainPlugin) {
//        Object[] sss = project.getTasks().toArray();
//        System.out.println(">>>>>>>"+ Arrays.toString(sss));
        Project project = mainPlugin.getProject();
        UploadTask upload = project.getTasks().create(taskName, UploadTask.class);
        project.getTasks().all(new Action<Task>() {
            @Override
            public void execute(Task task) {
                String name = task.getName();
                if (name != null && name.equalsIgnoreCase(taskName)) {
                    task.setGroup("upload");
                }
            }
        });
//        upload.doFirst(new Action<Task>() {
//            @Override
//            public void execute(Task task) {
//
//            }
//        });

        project.getConfigurations().all(new Action<Configuration>() {
            @Override
            public void execute(Configuration configuration) {
                if (configuration.getName().equalsIgnoreCase("archives")) {
                    Logc.e(configuration.getName() + "=======init=====>" + configuration.getArtifacts());
                    upload.setConfiguration(configuration);
                }
            }
        });


//        Configuration con = project.getConfigurations().getAt(taskName);
//        Logc.e("=======init=====>"+con);
//        upload.setConfiguration(project.getConfigurations().getAt(taskName));


//        project.getTasks().create("aaBuild", BuildTask.class);
//        project.getTasks().create("aaClean", CleanTask.class);
//        project.getTasks().create("aaTest", TestTask.class);

        /*Task clean = project.getTasks().getByName("clean");
        Logc.e("------------"+clean.getName());
        clean.dependsOn("aaTest");*/

//        Task clean = project.getTasks().getByName("clean");
//        if (clean != null) {
//            clean.dependsOn("aaClean");
//        }
//        Task build = project.getTasks().getByName("build");
//        if (build != null) {
//            build.dependsOn("aaBuild");
//        }
//        Task aaUpload = project.getTasks().getByName("uploadArchives");
//        if (aaUpload != null) {
//            aaUpload.dependsOn("aaUpload");
//        }


//        NamedDomainObjectList<MavenResolver> maven = project.getRepositories().withType(MavenResolver.class);
//        System.out.println("################"+maven.toString());
    }
}
